package com.example.sunmiv1.adapter;

import android.content.Context;
import android.content.res.Resources;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.example.sunmiv1.R;
import com.example.sunmiv1.entity.ReportLossBean;
import com.example.sunmiv1.utils.MathUtil;

import java.text.DecimalFormat;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class ReportLossAdapter extends BaseAdapter {

    private Context context;
    private List<ReportLossBean.DataBean> list;
    ViewHolder viewHolder;
    private DecimalFormat decimalFormat = new DecimalFormat("#0.000");

    public ReportLossAdapter(Context context, List<ReportLossBean.DataBean> list) {
        this.context = context;
        this.list = list;
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if (convertView == null) {
            convertView = LayoutInflater.from(context).inflate(R.layout.report_loss_adapter, null);
            viewHolder = new ViewHolder(convertView);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }
        Resources resources = context.getResources();
        viewHolder.reportLossAdapterName.setText(resources.getString(R.string.reportLoss_name) + list.get(position).getGoods_name());
        viewHolder.reportLossAdapterBarcode.setText(resources.getString(R.string.reportLoss_barcode) + list.get(position).getGoods_code());
        viewHolder.reportLossAdapterFreightNumber.setText(resources.getString(R.string.reportLoss_freightNumber) + list.get(position).getGood_number());
        viewHolder.reportLossAdapterPurchasePrice.setText(resources.getString(R.string.reportLoss_purchasePrice) + list.get(position).getPurch_price());
        viewHolder.reportLossAdapterRetailPurchases.setText(resources.getString(R.string.reportLoss_retailPurchases) + list.get(position).getNum());
        viewHolder.reportLossAdapterGive.setText(resources.getString(R.string.reportLoss_give) + list.get(position).getGivenum());
        viewHolder.reportLossAdapterSubtotal.setText(resources.getString(R.string.reportLoss_subtotal) + MathUtil.mul(Double.parseDouble(list.get(position).getPurch_price()),
                Double.parseDouble(list.get(position).getNum())));
        return convertView;
    }

    static class ViewHolder {
        @BindView(R.id.reportLoss_adapter_name)
        TextView reportLossAdapterName;
        @BindView(R.id.reportLoss_adapter_barcode)
        TextView reportLossAdapterBarcode;
        @BindView(R.id.reportLoss_adapter_freightNumber)
        TextView reportLossAdapterFreightNumber;
        @BindView(R.id.reportLoss_adapter_purchasePrice)
        TextView reportLossAdapterPurchasePrice;
        @BindView(R.id.reportLoss_adapter_retailPurchases)
        TextView reportLossAdapterRetailPurchases;
        @BindView(R.id.reportLoss_adapter_give)
        TextView reportLossAdapterGive;
        @BindView(R.id.reportLoss_adapter_subtotal)
        TextView reportLossAdapterSubtotal;

        ViewHolder(View view) {
            ButterKnife.bind(this, view);
        }
    }
}
